#!/bin/sh
#
# commit前eslint校验提交文件中js文件
# 若校验有error则阻止commit
# 命令行执行设置 git config --global core.hooksPath ./.githooks
#
# Author: waiting
# Date: 2017.02.09
#


if git rev-parse --verify HEAD >/dev/null 2>&1
then
  against=HEAD
else
  # Initial commit: diff against an empty tree object
  against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

branch=$( git branch | grep \* | cut -d ' ' -f2- )
if [[ $branch != "main" ]]; then
  exit 0
fi

# ------------- parse ts
files=$(git diff --cached --name-only --diff-filter=ACMR $against|grep '\.ts$')
if [[ $files != "" ]] ; then
  # echo $files | xargs eslint --fix 
  npm run lint
  rc=$?
  if [[ $rc != 0 ]] ; then
    echo -e "拒绝: ESLint 语法校验失败!"
    echo -e "ESLint错误代码信息参考 https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin/ "
    echo -e ""
    exit $rc
  fi
  git add $files
fi

exit 0
